package com.example.birdfriends.utils.export;


import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DownloadUtil {
    public static void download(HttpServletResponse response, String pathAddress) throws UnsupportedEncodingException {
        File file = new File(pathAddress);
        String fileName = file.getName();

        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso-8859-1"));

        byte[] buff = new byte[1024];
        FileInputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            inputStream = new FileInputStream(file);
            int readTmp = 0;
            while ((readTmp = inputStream.read(buff)) != -1) {
                outputStream.write(buff, 0, readTmp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("下载成功");
    }
}
